package shell.bootstraps.service
{
	import com.hobbit.analyze.AssetsManager;
	import com.hobbit.analyze.core.IConfigManager;
	import com.hobbit.analyze.event.AssetsEvent;
	
	import org.robotlegs.utilities.modular.mvcs.StarlingModuleActor;
	
	import shell.bootstraps.signals.SetupNetConfig;
	
	public class PreloadService extends StarlingModuleActor implements IPreloadService
	{
		[Inject]
		public var assetsManager:AssetsManager;
		
		[Inject]
		public var setupNetConfigSignal:SetupNetConfig;
		
		[Inject]
		public var configManager:IConfigManager;
		
		public function PreloadService()
		{
			super();
		}
		
		public function preload():void
		{
			assetsManager.setInitConfig(["ini.xml"]); // 加载所有预加载数据
			assetsManager.addEventListener(AssetsEvent.LOADING_COMPLETE,onLoadingComplete);
			assetsManager.addEventListener(AssetsEvent.MAIN_COMPLETE,onMainComplete);
			assetsManager.addEventListener(AssetsEvent.MAIN_PROGRESS,onMainProgress);
			assetsManager.addEventListener(AssetsEvent.CONFIG_COMPLETE,onConfigComplete);
		}
		
		private function onLoadingComplete(e:AssetsEvent = null):void
		{
			assetsManager.removeEventListener(AssetsEvent.LOADING_COMPLETE,onLoadingComplete);
			assetsManager.loadMainMoudle();
		}
		
		/**
		 * 预加载素材 加载进度 
		 * @param e
		 * 
		 */		
		private function onMainProgress(e:AssetsEvent):void
		{
			trace("MainProgress>>>>>>>>>>",e.progress);
		}
		
		private function onMainComplete(e:AssetsEvent):void
		{
			trace(">>>>>MainComplete>>>>>");
			
			assetsManager.removeEventListener(AssetsEvent.MAIN_PROGRESS,onMainProgress);
			assetsManager.removeEventListener(AssetsEvent.MAIN_COMPLETE,onMainComplete);
			configManager.parserDataXML();
			// 设置通讯配置
			setupNetConfigSignal.dispatch();
		}
		
		private function onConfigComplete(event:AssetsEvent):void
		{
			assetsManager.removeEventListener(AssetsEvent.CONFIG_COMPLETE,onConfigComplete);
		}
	}
}